//
//  CMDetailViewController.m
//  项目三
//
//  Created by rimi1 on 2016/11/26.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "DetailViewController.h"
#import "SignInViewController.h"

#import "CMDetailHeadView.h"
#import "CMDetailFoodView.h"
#import "NotView.h"

#import "NetRequest+Menu.h"
#import "AVRequest+Relation.h"

@interface DetailViewController ()

@property (nonatomic, strong) CMDetailHeadView *headView;
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) CMDetailFoodView *footView;
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) NotView *notView;

@end

@implementation DetailViewController

- (void)viewWillAppear:(BOOL)animated {
    if ([MyUser sharedUser].isSignIn) {
        [self urlIsCollectRequest];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    self.dataDic = [NSDictionary dictionary];
    [self urlMenuShowRequest];
}

- (void)initializeAppearance {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.webView.scrollView addSubview:self.headView];
    [self.view addSubview:self.webView];
    [self.view addSubview:self.footView];
}
// 初始化头部视图，图片。。
- (void)initializeHeadView {
    self.headView.name.text = self.dataDic[@"name"];
    NSURL *imageUrl = [NSURL URLWithString:self.dataDic[@"img"]];
    [self.headView.imageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"默认加载图"] options:SDWebImageProgressiveDownload];
}

#pragma mark - URLReqeust

- (void)urlMenuShowRequest {
    [NetRequest getMenuShowRequestWithParameter:@{@"id":self.menuId} succeed:^(id response) {
        if ([response[@"status"] integerValue] == 1) {
            [self.notView removeFromSuperview];
            NSMutableDictionary *dic = [response mutableCopy];
            dic[@"img"] = [NSString stringWithFormat:@"%@%@", IMAGE_URL, dic[@"img"]];
            NSString *str = [NSString stringWithFormat:@"%ld", [dic[@"id"] integerValue]];
            dic[@"id"] = str;
            self.dataDic = dic;
            [self initializeHeadView];
            [self.webView loadHTMLString:self.dataDic[@"message"] baseURL:nil];
        }else {
            [self.view insertSubview:self.notView belowSubview:self.footView];
        }
        
    } failure:^(NSError *error) {
        [self.view insertSubview:self.notView belowSubview:self.footView];
    }];
}

- (void)urlIsCollectRequest {
    [AVRequest avIsCollectWithMenuId:[NSString stringWithFormat:@"%@", self.menuId] query:^(NSArray *objects, NSError *error) {
        if (error) {
            return;
        }
        
        if (objects.count == 0) {
            self.footView.collect.selected = NO;
        }else {
            self.footView.collect.selected = YES;
        }
    }];
}

// 删除收藏
- (void)urlRemoveRequest {
    [AVRequest avRemoveCollectWithMenuId:self.dataDic[@"id"] removeSucceed:^(BOOL succeed, NSError *error) {
        if (error) {
            [Alert alertWithTitle:@"取消收藏失败" message:nil];
            return;
        }
        
        if (succeed) {
            self.footView.collect.selected = NO;
            [Alert alertWithTitle:@"取消收藏成功" message:nil];
        }else {
            [Alert alertWithTitle:@"取消收藏失败" message:nil];
        }
        
    }];
}
// 添加收藏
- (void)urlAddRequest {
    [AVRequest avAddCollectWithDynamicObj:nil menuId:self.dataDic[@"id"] name:self.dataDic[@"name"] food:self.dataDic[@"food"] img:self.dataDic[@"img"] addSucceed:^(BOOL succeed, NSError *error) {
        if (error) {
            [Alert alertWithTitle:@"收藏失败" message:nil];
            return;
        }
        
        if (succeed) {
            self.footView.collect.selected = YES;
            [Alert alertWithTitle:@"收藏成功" message:nil];
        }else {
            [Alert alertWithTitle:@"收藏失败" message:nil];
        }
        
    }];
}

#pragma mark - respodnsTo...

- (void)respondsToFootButton:(UIButton *)sender {
    switch (sender.tag) {
        case 100:{
            [self.navigationController popViewControllerAnimated:YES];
        }
            break;
            
        case 101:{
            if ([MyUser sharedUser].isSignIn) {
                if (self.footView.collect.selected) {
                    [self urlRemoveRequest];
                }else {
                    [self urlAddRequest];
                }
                
            }else {
                SignInViewController *signIn = [[SignInViewController alloc] init];
                [self.navigationController pushViewController:signIn animated:YES];
            }
            
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - get

- (CMDetailHeadView *)headView {
    
    if (!_headView) {
        
        CGFloat height = WXQAdaptionHeight(330);
        _headView = [[CMDetailHeadView alloc] initWithFrame:CGRectMake(0, -height, kWIDTH, height)];
        
    }
    return _headView;
}

- (UIWebView *)webView {
    
    if (!_webView) {
        // y置顶
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT - 49)];
        // 是否 不透明(不设这个值背景始终都是白色的，设置了的话，可以直接设置他的背景颜色)
        _webView.opaque = NO;
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.scrollView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.headView.bounds), 0, 0, 0);
        _webView.scrollView.bounces = NO;
        
    }
    return _webView;
}

- (CMDetailFoodView *)footView {
    
    if (!_footView) {
        
        _footView = [[CMDetailFoodView alloc] initWithFrame:CGRectMake(0, kHEIGHT - 49, kWIDTH, 49)];
        _footView.layer.shadowOffset = CGSizeMake(0, -5);
        _footView.layer.shadowOpacity = 0.15;
        __weak DetailViewController *detail = self;
        _footView.buttonBlock = ^(UIButton *sender) {
            [detail respondsToFootButton:sender];
        };
        _footView.backgroundColor = [UIColor whiteColor];
        
    }
    return _footView;
}

- (NotView *)notView {
    
    if (!_notView) {
        
        _notView = [[NotView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT - 49)];
        _notView.backgroundColor = [UIColor whiteColor];
        _notView.title.text = @"加载出错....";
        
        __weak DetailViewController *detail = self;
        _notView.tapRefresh = ^() {
            [detail urlMenuShowRequest];
        };
        
    }
    return _notView;
}

@end











